package com.koi.logs.ip;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static com.koi.common.utils.req.ServletUtil.getRequestAttributes;
import static com.koi.common.utils.string.StringUtil.inStringIgnoreCase;

/**
 * @author ･ᴗ･
 * @description [客户端工具类-登录使用获取request]
 */
public class ServletUtil {

    /**
     * @description [获取 HttpServletRequest 对象]
     * @author ･ᴗ･
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    /**
     * @description [获取 HttpServletResponse 对象]
     * @author ･ᴗ･
     */
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    /**
     * @description [获取 HttpServletSession 对象]
     * @author ･ᴗ･
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * @description [判断是否为 Ajax 请求]
     * @author ･ᴗ･
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.contains("application/json")) {
            return true;
        }

        String xRequestedWith = request.getHeader("X-Requested-With");
        if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
            return true;
        }

        String uri = request.getRequestURI();
        if (inStringIgnoreCase(uri, ".json", ".xml")) {
            return true;
        }

        String ajax = request.getParameter("__ajax");
        if (inStringIgnoreCase(ajax, "json", "xml")) {
            return true;
        }
        return false;
    }

    /**
     * @description [获取请求地址]
     * @author ･ᴗ･
     */
    public static String getRequestURI() {
        return getRequest().getRequestURI();
    }

    /**
     * @description [获取客户端地址]
     * @author ･ᴗ･
     */
    public static String getRemoteHost() {
        HttpServletRequest request = getRequest();
        try {
            System.out.println(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-real-ip");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip == null) {
            ip = "0.0.0.0";
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    /**
     * @description [获取当前请求方法]
     * @author ･ᴗ･
     */
    public static String getMethod() {
        return getRequest().getMethod();
    }

    /**
     * @description [获取请求头]
     * @author ･ᴗ･
     */
    public static String getHeader(String name) {
        return getRequest().getHeader(name);
    }

    /**
     * @description [获取 UserAgent]
     * @author ･ᴗ･
     */
    public static String getAgent() {
        return getHeader("User-Agent");
    }

    /**
     * @description [获取浏览器类型]
     * @author ･ᴗ･
     */
    public static String getBrowser() {
        String userAgent = getAgent();
        if (userAgent.contains("Firefox")) {
            return "火狐浏览器";
        } else if (userAgent.contains("Chrome")) {
            return "谷歌浏览器";
        } else if (userAgent.contains("Trident")) {
            return "IE 浏览器";
        } else {
            return "其他浏览器";
        }
    }

    /**
     * @description [获取系统类型]
     * @author ･ᴗ･
     */
    public static String getSystem() {
        String userAgent = getAgent();
        if (userAgent.toLowerCase().contains("windows")) {
            return "Windows";
        } else if (userAgent.toLowerCase().contains("mac")) {
            return "Mac";
        } else if (userAgent.toLowerCase().contains("x11")) {
            return "Unix";
        } else if (userAgent.toLowerCase().contains("android")) {
            return "Android";
        } else if (userAgent.toLowerCase().contains("iphone")) {
            return "IPhone";
        } else {
            return "UnKnown, More-Info: " + userAgent;
        }
    }

}
